package com.aizhixin.lab.resource.service;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.dto.SortDTO;
import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.course.course.entity.*;
import com.aizhixin.lab.course.course.repository.*;
import com.aizhixin.lab.course.course.service.TeacherCourseAssistService;
import com.aizhixin.lab.course.template.entity.*;
import com.aizhixin.lab.course.template.repository.*;
import com.aizhixin.lab.resource.domain.*;
import com.aizhixin.lab.resource.entity.Dir;
import com.aizhixin.lab.resource.entity.DirFile;
import com.aizhixin.lab.resource.repository.DirFileRepository;
import com.aizhixin.lab.resource.repository.DirRepository;
import com.aizhixin.lab.task.domain.QueryDomain;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class CourseWareService   {
    @Autowired
    private PageJdbcUtil pageJdbcUtil;
    @Autowired
    private CourseProfRepository courseProfRepository;
    @Autowired
    private CourseFileRepository courseFileRepository;
    @Autowired
    private CourseRepository courseRepository;

    @Autowired
    private CourseChapterRepository courseChapterRepository;
    @Autowired
    private CourseChapterFileRepository courseChapterFileRepository;

    @Autowired
    private TemplateProfRepository templateProfRepository;
    @Autowired
    private TemplateRepository templateRepository;
    @Autowired
    private TemplateChapterRepository templateChapterRepository;
    @Autowired
    private TemplateChapterFileRepository templateChapterFileRepository;
    @Autowired
    private TemplateFileRepository templateFileRepository;
    @Autowired
    private DirRepository dirRepository;
    @Autowired
    private DirFileRepository dirFileRepository;
    @Autowired
    private VideoService videoService;


    RowMapper<CollegeResourceDomain> collRm = new RowMapper<CollegeResourceDomain>() {

        @Override
        public CollegeResourceDomain mapRow(ResultSet rs, int rowNum) throws SQLException {
            // TODO Auto-generated method stub
            CollegeResourceDomain domain = new CollegeResourceDomain();
            domain.setId(rs.getLong("ID") + "");
            domain.setName(rs.getString("name"));
            domain.setCreatName(rs.getString("create_name"));
            domain.setCreatTime(rs.getDate("created_date"));
            return domain;
        }
    };

    /**
     * 获取学院列表
     *
     * @param domain
     * @param <T>
     * @return
     */
    public <T> Map<String, Object> getCollegeList(QueryDomain domain) {
        String sql = "SELECT tc.ID , tc.`NAME` name,tu.`name` create_name,tc.CREATED_DATE created_date FROM t_college tc LEFT JOIN t_user tu ON tc.CREATED_BY=tu.id WHERE tc.DELETE_FLAG=0";
//        String sql ="SELECT tc.ID , tc.`NAME` name,tu.`name` create_name,tc.CREATED_DATE created_date,count(*) num FROM c_course_chapter_file cccf LEFT JOIN c_course_chapter ccc ON cccf.chapter_id=ccc.id LEFT JOIN c_course cc ON ccc.course_id=cc.id LEFT JOIN c_course_prof ccp ON ccp.course_id=cc.id LEFT JOIN t_professional tp ON tp.ID=ccp.prof_id LEFT JOIN t_college tc ON tc.ID=tp.COLLEGE_ID LEFT JOIN t_user tu ON tu.id = tc.CREATED_BY GROUP BY tc.ID";
        String count = "SELECT COUNT(*) FROM t_college tc LEFT JOIN t_user tu ON tc.CREATED_BY=tu.id WHERE tc.DELETE_FLAG=0";
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto.setKey("tc.CREATED_DATE");
        dto.setAsc(false);
        sort.add(dto);
        Map<String, Object> pageInfor = pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), collRm, sort, sql, count);
        List<CollegeResourceDomain> domainList = (List<CollegeResourceDomain>) pageInfor.get(ApiReturnConstants.DATA);
        List<CollegeResourceDomain> domainList1 = new ArrayList<>();
        int i = 0;
        for (CollegeResourceDomain resourceDomain : domainList) {
            Integer collegeFileNum = getCollegeFileNum(Long.parseLong(resourceDomain.getId()), domain.getType());
            CollegeResourceDomain resourceDomain1 = domainList.get(i);
            resourceDomain1.setFileNum(collegeFileNum);
            domainList1.add(resourceDomain1);
            i++;
        }
        pageInfor.put(ApiReturnConstants.DATA, domainList1);
        return pageInfor;
    }

    /**
     * 获取学院下某种类型的文件数量
     *
     * @param collegeId 学院Id
     * @param type      文件类型
     * @return
     */
    public Integer getCollegeFileNum(Long collegeId, Integer type) {
        String countSql;
        if(type==10){
            countSql="SELECT COUNT(DISTINCT a.num) FROM( SELECT cccf.id num FROM c_template_chapter_file cccf LEFT JOIN c_template_chapter ccc ON cccf.chapter_id=ccc.id LEFT JOIN c_template cc ON ccc.template_id=cc.id LEFT JOIN c_template_prof ccp ON ccp.template_id=cc.id LEFT JOIN t_professional tp ON tp.ID=ccp.prof_id LEFT JOIN t_college tc ON tc.ID=tp.COLLEGE_ID WHERE tc.ID= "+collegeId+" AND cccf.type ="+type+" UNION ALL SELECT ccf.id num FROM c_template_file ccf LEFT JOIN c_template cc ON ccf.template_id=cc.id  LEFT JOIN c_template_prof ccp ON ccp.template_id=cc.id LEFT JOIN t_professional tp ON ccp.prof_id=tp.ID LEFT JOIN t_college tc ON tc.ID=tp.COLLEGE_ID WHERE ccf.delete_flag=0 and tc.ID ="+collegeId+" ) a";
        } else if (type==60){
            countSql="SELECT COUNT(DISTINCT cca.id) FROM c_template_assist cca LEFT JOIN c_template cc ON cca.template_id=cc.id  LEFT JOIN c_template_prof ccp ON ccp.template_id=cc.id LEFT JOIN t_professional tp ON ccp.prof_id=tp.ID LEFT JOIN t_college tc ON tc.ID=tp.COLLEGE_ID WHERE cca.delete_flag=0 and tc.ID ="+collegeId;
        }
        else {
            countSql = "SELECT count(DISTINCT cccf.id) num FROM c_template_chapter_file cccf LEFT JOIN c_template_chapter ccc ON cccf.chapter_id=ccc.id LEFT JOIN c_template cc ON ccc.template_id=cc.id LEFT JOIN c_template_prof ccp ON ccp.template_id=cc.id LEFT JOIN t_professional tp ON tp.ID=ccp.prof_id LEFT JOIN t_college tc ON tc.ID=tp.COLLEGE_ID WHERE tc.ID= "+collegeId+" AND cccf.type =" + type;
        }
        return pageJdbcUtil.getCountInt(countSql);
    }

    /**
     * 获取专业下文件数量
     * @param profId 专业Id
     * @param type
     * @return
     */
    public Integer getProfFileNum(Long profId, Integer type) {
        String countSql;
        if (type==10){
            countSql="SELECT COUNT(*) FROM( SELECT cccf.id num FROM c_template_chapter_file cccf LEFT JOIN c_template_chapter ccc ON cccf.chapter_id=ccc.id LEFT JOIN c_template cc ON ccc.template_id=cc.id LEFT JOIN c_template_prof ccp ON ccp.template_id=cc.id LEFT JOIN t_professional tp ON ccp.prof_id=tp.ID WHERE tp.ID= "+profId+" AND cccf.type ="+type+" AND cccf.delete_flag=0 UNION ALL SELECT ccf.id num FROM c_template_file ccf LEFT JOIN c_template cc ON ccf.template_id=cc.id  LEFT JOIN c_template_prof ccp ON ccp.template_id=cc.id LEFT JOIN t_professional tp ON ccp.prof_id=tp.ID  WHERE ccf.delete_flag=0 and tp.ID = "+profId+") a";
        } else if (type==60){
            countSql="SELECT COUNT(*) FROM c_template_assist cca LEFT JOIN c_template cc ON cca.template_id=cc.id  LEFT JOIN c_template_prof ccp ON ccp.template_id=cc.id LEFT JOIN t_professional tp ON ccp.prof_id=tp.ID  WHERE cca.delete_flag=0 AND tp.ID="+profId;
        }
        else {
             countSql = "SELECT count(*) num FROM c_template_chapter_file cccf LEFT JOIN c_template_chapter ccc ON cccf.chapter_id=ccc.id LEFT JOIN c_template cc ON ccc.template_id=cc.id LEFT JOIN c_template_prof ccp ON ccp.template_id=cc.id LEFT JOIN t_professional tp ON tp.ID=ccp.prof_id  WHERE tp.ID= "+profId+" AND cccf.type =" + type;
        }
        return pageJdbcUtil.getCountInt(countSql);
    }

    /**
     * 获取课程下某种类型的文件数
     * @param courseId
     * @param type
     * @return
     */
    public Integer getCourseChapterFileNum(String courseId, Integer type){
        String countSql;
        if (type==10){
           countSql="SELECT COUNT(*) num FROM (SELECT cccf.id FROM c_course_chapter_file cccf LEFT JOIN c_course_chapter ccc ON cccf.chapter_id=ccc.id LEFT JOIN c_course cc ON ccc.course_id=cc.id WHERE cccf.delete_flag=0 AND cccf.type="+type+" AND cc.id='"+courseId+"' UNION ALL SELECT ccf.id FROM c_course_file ccf WHERE ccf.delete_flag=0 AND ccf.course_id='"+courseId+"') a";
        }else if (type==60){
            countSql="SELECT COUNT(*) num FROM c_course_assist cca WHERE cca.delete_flag=0 AND cca.course_id= '" +courseId+"'";
        }
        else {
            countSql = "SELECT COUNT(*) num FROM c_course_chapter_file cccf LEFT JOIN c_course_chapter ccc ON cccf.chapter_id=ccc.id LEFT JOIN c_course cc ON ccc.course_id=cc.id WHERE cccf.delete_flag=0 AND cccf.type="+type+" AND cc.id='"+courseId+"'";
        }
        return pageJdbcUtil.getCountInt(countSql);
    }

    /**
     * 获取模板下某种类型的文件数
     * @param templateId
     * @param type
     * @return
     */
    public Integer getTemplateChapterFileNum(Long templateId, Integer type){
        String countSql;
        if (type==10){
            countSql="SELECT COUNT(*) FROM (SELECT ctcf.id FROM c_template_chapter_file ctcf LEFT JOIN c_template_chapter ctc ON ctc.id=ctcf.chapter_id WHERE ctcf.delete_flag=0 AND ctcf.type="+type+" AND ctc.template_id="+templateId+" UNION ALL SELECT cca.id FROM c_template_file cca WHERE cca.delete_flag=0 AND cca.template_id="+templateId+") a";
        }else if (type==60){
             countSql="SELECT COUNT(*) FROM c_template_assist cca WHERE cca.delete_flag=0 AND cca.template_id= " +templateId;
        }else {
             countSql="SELECT COUNT(*) FROM c_template_chapter_file ctcf LEFT JOIN c_template_chapter ctc ON ctc.id=ctcf.chapter_id WHERE ctcf.delete_flag=0 AND ctcf.type="+type+" AND ctc.template_id="+templateId ;
        }
        return pageJdbcUtil.getCountInt(countSql);
    }

    /**
     * 获取文件夹下某种类型的文件数
     * @param dirId
     * @param type
     * @return
     */
    public Integer getDirFileNum(Long dirId, Integer type){
        String countSql="SELECT COUNT(*) FROM c_dir_file WHERE  c_dir_file.type="+type+" AND c_dir_file.delete_flag=0 and c_dir_file.dir_id = "+ dirId;

        return pageJdbcUtil.getCountInt(countSql);
    }

    /**
     * 获取课程资料文件总数
     * @return
     */
    public Integer getCourseFileNum(){
        String countSql="SELECT count(*) FROM (SELECT ct.id,ct.delete_flag FROM c_template_file ct) a WHERE a.delete_flag=0";
        return pageJdbcUtil.getCountInt(countSql);
    }
    /**
     * 获取专业列表
     *
     * @param domain
     * @param <T>
     * @return
     */
    public <T> Map<String, Object> getProfessionalList(QueryDomain domain) {
        String sql = "SELECT tp.ID , tp.`NAME` name,tu.`name` create_name,tp.CREATED_DATE created_date FROM t_professional tp LEFT JOIN t_user tu on tp.CREATED_BY=tu.id WHERE tp.DELETE_FLAG=0 AND tp.COLLEGE_ID= " + domain.getId();
        String count = "SELECT COUNT(*) FROM t_professional tp LEFT JOIN t_user tu on tp.CREATED_BY=tu.id WHERE tp.DELETE_FLAG=0 AND tp.COLLEGE_ID= " + domain.getId();
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto.setKey("tp.CREATED_DATE");
        dto.setAsc(false);
        sort.add(dto);
        Map<String, Object> pageInfor = pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), collRm, sort, sql, count);
        List<CollegeResourceDomain> domainList = (List<CollegeResourceDomain>) pageInfor.get(ApiReturnConstants.DATA);
        List<CollegeResourceDomain> domainList1 = new ArrayList<>();
        for (CollegeResourceDomain resourceDomain : domainList) {
            Integer collegeFileNum = getProfFileNum(Long.parseLong(resourceDomain.getId()), domain.getType());
            resourceDomain.setFileNum(collegeFileNum);
            domainList1.add(resourceDomain);
        }
        pageInfor.put(ApiReturnConstants.DATA, domainList1);
        return pageInfor;
    }

    RowMapper<CollegeResourceDomain> courseRm = new RowMapper<CollegeResourceDomain>() {

        @Override
        public CollegeResourceDomain mapRow(ResultSet rs, int rowNum) throws SQLException {
            // TODO Auto-generated method stub
            CollegeResourceDomain domain = new CollegeResourceDomain();
            domain.setId(rs.getString("table_name") + "_" + rs.getString("ID"));
            domain.setName(rs.getString("name"));
            domain.setCreatName(rs.getString("created_name"));
            domain.setCreatTime(rs.getDate("created_date"));
            domain.setEdit(rs.getString("is_edit"));
            return domain;
        }
    };

    /**
     * 获取课程列表和文件夹列表
     *
     * @param domain
     */
    public <T> Map<String, Object> findCourseList(QueryDomain domain,Long createdBy) {
        String sql = "SELECT * FROM (SELECT ct.id id,ct.`name` name,tu.`name` created_name,ct.created_date created_date ,'T' table_name,ctp.prof_id prof_id,ct.delete_flag delete_flag,(CASE ct.created_by WHEN "+createdBy+" THEN '编辑' ELSE '不编辑' END) is_edit FROM c_template ct LEFT JOIN t_user tu ON ct.created_by = tu.id LEFT JOIN c_template_prof ctp ON ctp.template_id=ct.id UNION ALL SELECT cd.id id,cd.dir_name name,tu.`name` created_name,cd.created_date created_date,'D' table_name,cd.prof_id prof_id,cd.delete_flag delete_flag,(CASE cd.created_by WHEN "+createdBy+" THEN '编辑' ELSE '不编辑' END) is_edit FROM c_dir cd LEFT JOIN t_user tu ON cd.created_by=tu.id ) a WHERE a.delete_flag=0 ";
        String countSql = "SELECT count(*) FROM (SELECT ct.id id,ct.`name` name,tu.`name` created_name,ct.created_date created_date ,'T' table_name,ctp.prof_id prof_id,ct.delete_flag delete_flag FROM c_template ct LEFT JOIN t_user tu ON ct.created_by = tu.id LEFT JOIN c_template_prof ctp ON ctp.template_id=ct.id UNION ALL SELECT cd.id id,cd.dir_name name,tu.`name` created_name,cd.created_date created_date,'D' table_name,cd.prof_id prof_id,cd.delete_flag delete_flag FROM c_dir cd LEFT JOIN t_user tu ON cd.created_by=tu.id ) a WHERE a.delete_flag=0 ";
        if (!StringUtils.isEmpty(domain.getId())) {
            sql += " and a.prof_id= " + domain.getId();
            countSql += " and a.prof_id= " + domain.getId();
        }
        if (!StringUtils.isEmpty(domain.getName())){
            sql +="AND a.`name` LIKE '%"+domain.getName()+"%'";
            countSql +="AND a.`name` LIKE '%"+domain.getName()+"%'";
        }
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto.setKey("a.CREATED_DATE");
        dto.setAsc(false);
        sort.add(dto);
        Map<String, Object> pageInfor = pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), courseRm, sort, sql, countSql);
        List<CollegeResourceDomain> domainList = (List<CollegeResourceDomain>) pageInfor.get(ApiReturnConstants.DATA);
        List<CollegeResourceDomain> domainList1 = new ArrayList<>();
        for (CollegeResourceDomain resourceDomain : domainList) {
            String[] s = resourceDomain.getId().split("_");
            Integer fileNum=0;
            if ("C".equals(s[0])){
                fileNum = getCourseChapterFileNum(s[1], domain.getType());
            }
            if ("T".equals(s[0])){
                fileNum = getTemplateChapterFileNum(Long.parseLong(s[1]), domain.getType());
            }
            if ("D".equals(s[0])){
                fileNum=  getDirFileNum(Long.parseLong(s[1]), domain.getType());
            }
            resourceDomain.setFileNum(fileNum);
            domainList1.add(resourceDomain);
        }
        pageInfor.put(ApiReturnConstants.DATA, domainList1);
        return pageInfor;
    }

    RowMapper<FileInfoDomain> fileRm = new RowMapper<FileInfoDomain>() {

        @Override
        public FileInfoDomain mapRow(ResultSet rs, int rowNum) throws SQLException {
            // TODO Auto-generated method stub
            FileInfoDomain domain = new FileInfoDomain();
            domain.setId("C_" + rs.getString("ID"));
            domain.setFileName(rs.getString("file_name"));
            Long parenId = rs.getLong("parent_id");
            if (null != parenId && 0 != parenId) {
                domain.setChapterName(rs.getString("chapter_name"));
                domain.setMinutiaName(rs.getString("minutia_name"));
            } else {
                domain.setChapterName(rs.getString("minutia_name"));
            }
            domain.setCourseName(rs.getString("course_name"));
            domain.setCreateDate(rs.getDate("created_date"));

            return domain;
        }
    };
    RowMapper<FileInfoDomain> assistFileRm = new RowMapper<FileInfoDomain>() {

        @Override
        public FileInfoDomain mapRow(ResultSet rs, int rowNum) throws SQLException {
            // TODO Auto-generated method stub
            FileInfoDomain domain = new FileInfoDomain();
            String tableName=rs.getString("table_name");
            domain.setId(tableName+"_" + rs.getString("id"));
            domain.setFileName(rs.getString("file_name"));
            Long parenId = rs.getLong("parent_id");
            if (tableName.equals("C")){
            if (null != parenId && 0 != parenId) {
                domain.setChapterName(rs.getString("chapter_name"));
                domain.setMinutiaName(rs.getString("minutia_name"));
            } else {
                domain.setChapterName(rs.getString("minutia_name"));
            }
            }
            domain.setCourseName(rs.getString("course_name"));
            domain.setCreateDate(rs.getDate("created_date"));

            return domain;
        }
    };
    public <T> Map<String, Object> findCourseFile(QueryDomain domain) {

        String sql = "SELECT a.id,a.file_name file_name,a.parent_id parent_id,a.minutia_name minutia_name,a.chapter_name chapter_name,a.course_name course_name,a.created_date,a.id course_id,a.delete_flag,a.table_name FROM (SELECT cccf.id,cccf.`name` file_name,ccc1.parent_id parent_id,ccc1.`name` minutia_name,ccc2.`name` chapter_name,cc.`name` course_name,cccf.created_date,cc.id course_id,cccf.delete_flag,'C' table_name  FROM c_course_chapter_file cccf LEFT JOIN c_course_chapter ccc1 ON cccf.chapter_id=ccc1.id LEFT JOIN c_course_chapter ccc2 ON ccc1.parent_id =ccc2.id LEFT JOIN c_course cc ON ccc1.course_id=cc.id WHERE cccf.type=10 UNION ALL SELECT ccf.id,ccf.`name` file_name,0 parent_id,'' minutia_name,'' chapter_name,cc.`name` course_name,ccf.created_date created_date,cc.id course_id,ccf.delete_flag,'A' table_name FROM c_course_file ccf LEFT JOIN c_course cc ON ccf.course_id =cc.id) a WHERE a.delete_flag=0 ";
        String countSql = "SELECT count(*) FROM (SELECT cccf.id,cccf.`name` file_name,ccc1.parent_id parent_id,ccc1.`name` minutia_name,ccc2.`name` chapter_name,cc.`name` course_name,cccf.created_date,cc.id course_id,cccf.delete_flag,'C' table_name FROM c_course_chapter_file cccf LEFT JOIN c_course_chapter ccc1 ON cccf.chapter_id=ccc1.id LEFT JOIN c_course_chapter ccc2 ON ccc1.parent_id =ccc2.id LEFT JOIN c_course cc ON ccc1.course_id=cc.id WHERE cccf.type = 10 UNION ALL SELECT cca.id,cca.`name` file_name,0 parent_id,'' minutia_name,'' chapter_name,cc.`name` course_name,cca.created_date created_date,cc.id course_id,cca.delete_flag,'A' table_name FROM c_course_assist cca LEFT JOIN c_course cc ON cca.course_id =cc.id) a WHERE a.delete_flag=0";


        if (!StringUtils.isEmpty(domain.getId())) {
            String[] s = domain.getId().split("_");
            sql += " and a.course_id= '" + s[1]+"' ";
            countSql += " and a.course_id= '" + s[1]+"' ";
        }
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto.setAsc(false);
        dto.setKey("a.created_date");
        sort.add(dto);

        return pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), assistFileRm, sort, sql, countSql);
    }

    /**
     * 获取课程章节下面的文件
     *
     * @param domain 课程的Id和页码
     * @param
     * @param <T>
     * @return
     */
    public <T> Map<String, Object> findFile(QueryDomain domain) {

        String sql = "SELECT cccf.id,cccf.`name` file_name,ccc1.parent_id parent_id,ccc1.`name` minutia_name,ccc2.`name` chapter_name,cc.`name` course_name,cccf.created_date FROM c_course_chapter_file cccf LEFT JOIN c_course_chapter ccc1 ON cccf.chapter_id=ccc1.id LEFT JOIN c_course_chapter ccc2 ON ccc1.parent_id =ccc2.id LEFT JOIN c_course cc ON ccc1.course_id=cc.id WHERE cccf.delete_flag=0 and cccf.type = " + domain.getType() + " ";
        String countSql = "SELECT count(*) FROM c_course_chapter_file cccf LEFT JOIN c_course_chapter ccc1 ON cccf.chapter_id=ccc1.id LEFT JOIN c_course_chapter ccc2 ON ccc1.parent_id =ccc2.id LEFT JOIN c_course cc ON ccc1.course_id=cc.id WHERE cccf.delete_flag=0 and cccf.type = " + domain.getType() + " ";


        if (!StringUtils.isEmpty(domain.getId())) {
            String[] s = domain.getId().split("_");
            sql += " and cc.id= " + s[1];
            countSql += " and cc.id= " + s[1];
        }
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto.setAsc(false);
        dto.setKey("cccf.created_date");
        sort.add(dto);

        return pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), fileRm, sort, sql, countSql);
    }

    RowMapper<FileInfoDomain> temFileRm = new RowMapper<FileInfoDomain>() {

        @Override
        public FileInfoDomain mapRow(ResultSet rs, int rowNum) throws SQLException {
            // TODO Auto-generated method stub
            FileInfoDomain domain = new FileInfoDomain();
            domain.setId("T_" + rs.getLong("ID"));
            domain.setFileName(rs.getString("file_name"));
            Long parenId = rs.getLong("parent_id");
            if (null != parenId && 0 != parenId) {
                domain.setChapterName(rs.getString("chapter_name"));
                domain.setMinutiaName(rs.getString("minutia_name"));
            } else {
                domain.setChapterName(rs.getString("minutia_name"));
            }
            domain.setCourseName(rs.getString("course_name"));
            domain.setCreateDate(rs.getDate("created_date"));

            return domain;
        }
    };

    /**
     * 获取模板下的文件
     *
     * @param domain
     * @param
     * @param <T>
     * @return
     */
    public <T> Map<String, Object> findTemplateFile(QueryDomain domain) {
        String sql = "SELECT cccf.id,cccf.`name` file_name,ccc1.`name` minutia_name,ccc1.parent_id parent_id,ccc2.`name` chapter_name,cc.`name` course_name,cccf.created_date FROM c_template_chapter_file cccf LEFT JOIN c_template_chapter ccc1 ON cccf.chapter_id=ccc1.id LEFT JOIN c_template_chapter ccc2 ON ccc1.parent_id =ccc2.id LEFT JOIN c_template cc ON ccc1.template_id =cc.id WHERE cccf.delete_flag=0 and cccf.type = " + domain.getType() + " ";
        String countSql = "SELECT count(*) FROM c_template_chapter_file cccf LEFT JOIN c_template_chapter ccc1 ON cccf.chapter_id=ccc1.id LEFT JOIN c_template_chapter ccc2 ON ccc1.parent_id =ccc2.id LEFT JOIN c_template cc ON ccc1.template_id =cc.id WHERE cccf.delete_flag=0 and cccf.type = " + domain.getType() + " ";
        if (!StringUtils.isEmpty(domain.getId())) {
            String[] s = domain.getId().split("_");
            sql += " and cc.id= " + Long.parseLong(s[1]);
            countSql += " and cc.id= " + Long.parseLong(s[1]);
        }
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto.setAsc(false);
        dto.setKey("cccf.created_date");
        sort.add(dto);

        return pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), temFileRm, sort, sql, countSql);
    }
    RowMapper<FileInfoDomain> tCourseFileRm = new RowMapper<FileInfoDomain>() {

        @Override
        public FileInfoDomain mapRow(ResultSet rs, int rowNum) throws SQLException {
            // TODO Auto-generated method stub
            FileInfoDomain domain = new FileInfoDomain();
            String tableName=rs.getString("table_name");
            domain.setId(tableName+"_" + rs.getLong("id"));
            domain.setFileName(rs.getString("file_name"));
            Long parenId = rs.getLong("parent_id");
            if (tableName.equals("T")) {
                if (null != parenId && 0 != parenId) {
                    domain.setChapterName(rs.getString("chapter_name"));
                    domain.setMinutiaName(rs.getString("minutia_name"));
                } else {
                    domain.setChapterName(rs.getString("minutia_name"));
                }
            }
            domain.setCourseName(rs.getString("template_name"));
            domain.setCreateDate(rs.getDate("created_date"));

            return domain;
        }
    };
    /**
     * 搜索模板下的文件
     *
     * @param domain
     * @param
     * @param <T>
     * @return
     */
    public <T> Map<String, Object> searchFile(QueryDomain domain) {
        String sql = "SELECT cccf.id,cccf.`name` file_name,ccc1.`name` minutia_name,ccc1.parent_id parent_id,ccc2.`name` chapter_name,cc.`name` course_name,cccf.created_date FROM c_template_chapter_file cccf LEFT JOIN c_template_chapter ccc1 ON cccf.chapter_id=ccc1.id LEFT JOIN c_template_chapter ccc2 ON ccc1.parent_id =ccc2.id LEFT JOIN c_template cc ON ccc1.template_id =cc.id WHERE cccf.delete_flag=0 and cccf.type = " + domain.getType() + " ";
        String countSql = "SELECT count(*) FROM c_template_chapter_file cccf LEFT JOIN c_template_chapter ccc1 ON cccf.chapter_id=ccc1.id LEFT JOIN c_template_chapter ccc2 ON ccc1.parent_id =ccc2.id LEFT JOIN c_template cc ON ccc1.template_id =cc.id WHERE cccf.delete_flag=0 and cccf.type = " + domain.getType() + " ";
        if (!StringUtils.isEmpty(domain.getName())) {
            sql += " AND cccf.`name` LIKE '%"+domain.getName()+"%'";
            countSql += " AND cccf.`name` LIKE '%"+domain.getName()+"%'";
        }
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto.setAsc(false);
        dto.setKey("cccf.created_date");
        sort.add(dto);

        return pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), temFileRm, sort, sql, countSql);
    }
    /**
     * 搜索课程文件
     * @param domain
     * @param <T>
     * @return
     */
    public <T> Map<String, Object> searchCourseFile(QueryDomain domain) {
        String sql = "SELECT * FROM (SELECT ctcf.id,ctcf.`name` file_name,ctc1.parent_id parent_id,ctc1.`name` minutia_name,ctc2.`name` chapter_name,ct.`name` template_name,ctcf.created_date,ct.id template_id,ctcf.delete_flag,'T' table_name FROM c_template_chapter_file ctcf LEFT JOIN c_template_chapter ctc1 ON ctcf.chapter_id=ctc1.id LEFT JOIN c_template_chapter ctc2 ON ctc1.parent_id =ctc2.id LEFT JOIN c_template ct ON ctc1.template_id=ct.id WHERE ctcf.type = 10 UNION ALL SELECT ctf.id,ctf.`name` file_name,0 parent_id,'' minutia_name,'' chapter_name,ct.`name` template_name,ctf.created_date created_date,ct.id template_id,ctf.delete_flag, 'B' table_name FROM c_template_file ctf LEFT JOIN c_template ct ON ctf.template_id =ct.id) a WHERE a.delete_flag=0 ";
        String countSql = "SELECT count(*) FROM (SELECT ctcf.id,ctcf.`name` file_name,ctc1.parent_id parent_id,ctc1.`name` minutia_name,ctc2.`name` chapter_name,ct.`name` template_name,ctcf.created_date,ct.id template_id,ctcf.delete_flag,'T' table_name FROM c_template_chapter_file ctcf LEFT JOIN c_template_chapter ctc1 ON ctcf.chapter_id=ctc1.id LEFT JOIN c_template_chapter ctc2 ON ctc1.parent_id =ctc2.id LEFT JOIN c_template ct ON ctc1.template_id=ct.id WHERE ctcf.type = 10 UNION ALL SELECT ctf.id,ctf.`name` file_name,0 parent_id,'' minutia_name,'' chapter_name,ct.`name` template_name,ctf.created_date created_date,ct.id template_id,ctf.delete_flag, 'B' table_name FROM c_template_file ctf LEFT JOIN c_template ct ON ctf.template_id =ct.id) a WHERE a.delete_flag=0 ";
        if (!StringUtils.isEmpty(domain.getName())) {
            sql += " AND a.file_name LIKE '%"+domain.getName()+"%'";
            countSql += " AND a.file_name LIKE '%"+domain.getName()+"%'";
        }
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto.setAsc(false);
        dto.setKey("a.created_date");
        sort.add(dto);
        return pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), tCourseFileRm, sort, sql, countSql);
    }
    /**
     * 获取模板下的课程文件
     *
     * @param domain
     * @param
     * @param <T>
     * @return
     */
    public <T> Map<String, Object> findCourseTemplateFile(QueryDomain domain) {
        String sql = "SELECT * FROM (SELECT ctcf.id,ctcf.`name` file_name,ctc1.parent_id parent_id,ctc1.`name` minutia_name,ctc2.`name` chapter_name,ct.`name` template_name,ctcf.created_date,ct.id template_id,ctcf.delete_flag,'T' table_name FROM c_template_chapter_file ctcf LEFT JOIN c_template_chapter ctc1 ON ctcf.chapter_id=ctc1.id LEFT JOIN c_template_chapter ctc2 ON ctc1.parent_id =ctc2.id LEFT JOIN c_template ct ON ctc1.template_id=ct.id WHERE ctcf.type = 10 UNION ALL SELECT ctf.id,ctf.`name` file_name,0 parent_id,'' minutia_name,'' chapter_name,ct.`name` template_name,ctf.created_date created_date,ct.id template_id,ctf.delete_flag, 'B' table_name FROM c_template_file ctf LEFT JOIN c_template ct ON ctf.template_id =ct.id) a WHERE a.delete_flag=0 ";
        String countSql = "SELECT count(*) FROM (SELECT ctcf.id,ctcf.`name` file_name,ctc1.parent_id parent_id,ctc1.`name` minutia_name,ctc2.`name` chapter_name,ct.`name` template_name,ctcf.created_date,ct.id template_id,ctcf.delete_flag,'T' table_name FROM c_template_chapter_file ctcf LEFT JOIN c_template_chapter ctc1 ON ctcf.chapter_id=ctc1.id LEFT JOIN c_template_chapter ctc2 ON ctc1.parent_id =ctc2.id LEFT JOIN c_template ct ON ctc1.template_id=ct.id WHERE ctcf.type = 10 UNION ALL SELECT ctf.id,ctf.`name` file_name,0 parent_id,'' minutia_name,'' chapter_name,ct.`name` template_name,ctf.created_date created_date,ct.id template_id,ctf.delete_flag, 'B' table_name FROM c_template_file ctf LEFT JOIN c_template ct ON ctf.template_id =ct.id) a WHERE a.delete_flag=0 ";
        if (!StringUtils.isEmpty(domain.getId())) {
            String[] s = domain.getId().split("_");
            sql += " and a.template_id= " + Long.parseLong(s[1]);
            countSql += " and a.template_id= " + Long.parseLong(s[1]);
        }
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto.setAsc(false);
        dto.setKey("a.created_date");
        sort.add(dto);

        return pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), tCourseFileRm, sort, sql, countSql);
    }

    RowMapper<FileInfoDomain> dirFileRm = new RowMapper<FileInfoDomain>() {

        @Override
        public FileInfoDomain mapRow(ResultSet rs, int rowNum) throws SQLException {
            // TODO Auto-generated method stub
            FileInfoDomain domain = new FileInfoDomain();
            domain.setId("D_" + rs.getLong("id"));
            domain.setFileName(rs.getString("file_name"));
            domain.setCourseName(rs.getString("dir_name"));
            domain.setCreateDate(rs.getDate("created_date"));

            return domain;
        }
    };

    /**
     * 获取文件夹下的文件
     *
     * @param domain
     * @param
     * @param <T>
     * @return
     */
    public <T> Map<String, Object> findDirFile(QueryDomain domain) {

        String sql = "SELECT cdf.id,cdf.`name` file_name,cd.dir_name dir_name,cdf.created_date created_date FROM c_dir_file cdf LEFT JOIN c_dir cd ON cd.id = cdf.dir_id WHERE cdf.delete_flag=0 and cdf.type = " + domain.getType() + " ";
        String countSql = "SELECT count(*) FROM c_dir_file cdf LEFT JOIN c_dir cd ON cd.id = cdf.dir_id WHERE cdf.delete_flag=0 and cdf.type = " + domain.getType() + " ";

        if (!StringUtils.isEmpty(domain.getId())) {
            String[] s = domain.getId().split("_");
            sql += " and cd.id= " + Long.parseLong(s[1]);
            countSql += " and cd.id= " + Long.parseLong(s[1]);
        }
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto.setAsc(false);
        dto.setKey("cdf.CREATED_DATE");
        sort.add(dto);

        return pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), dirFileRm, sort, sql, countSql);
    }

    RowMapper<FileTotalNumDomain> fileTotalRm = new RowMapper<FileTotalNumDomain>() {

        @Override
        public FileTotalNumDomain mapRow(ResultSet rs, int rowNum) throws SQLException {
            // TODO Auto-generated method stub
            FileTotalNumDomain domain = new FileTotalNumDomain();
            domain.setCourseFileNum(rs.getInt("a"));
            domain.setVideoNum(rs.getInt("b"));
            domain.setTaskGuidanceFileNum(rs.getInt("c"));
            domain.setTaskFileNum(rs.getInt("d"));
            domain.setOtherFileNum(rs.getInt("e"));
            return domain;
        }
    };

    /**
     * 统计各种类型的文件数量
     *
     * @return
     */
    public List<FileTotalNumDomain> findFileTotal() {

        String sql = "SELECT SUM(a.type=10) a,SUM(a.type=20) b,SUM(a.type=30) c,SUM(a.type=40) d,SUM(a.type=50) e FROM(SELECT ctcf.type type,ctcf.delete_flag FROM c_template_chapter_file ctcf  UNION ALL SELECT cdf.type type,cdf.delete_flag FROM c_dir_file cdf) a WHERE a.delete_flag=0  ";


        return pageJdbcUtil.getInfo(sql, fileTotalRm);
    }

    /**
     * 获取专业下的所有课程列表和文件夹列表(添加文件时用)
     *
     * @param profId 专业ID
     * @return
     */
    public List<DirDomain> getFolderDomain(Long profId) {
        List<DirDomain> domainList = new ArrayList<>();
        List<TemplateProf> templateProfList = templateProfRepository.findByProfIdAndDeleteFlag(profId, 0);
        for (TemplateProf templateProf : templateProfList) {
            DirDomain domain = new DirDomain();
            Template template = templateRepository.findByIdAndDeleteFlag(templateProf.getTemplateId(), 0);
            if (template==null){
                continue;
            }
            String templateId = String.valueOf(template.getId());
            domain.setDirId("T_" + templateId);
            domain.setFolderName(template.getName());
            domainList.add(domain);
        }
        List<Dir> dirList = dirRepository.findByProfIdAndDeleteFlag(profId, 0);
        for (Dir dir : dirList) {
            String dirId = String.valueOf(dir.getId());
            DirDomain domain = new DirDomain();
            domain.setDirId("D_" + dirId);
            domain.setFolderName(dir.getDirName());
            domainList.add(domain);
        }
        return domainList;
    }

    /**
     * 获取课程下所以章名(添加文件时用)
     *
     * @param folderNoteId
     * @return
     */
    public List<CourseChapterDomain> getVideoChapterDomain(String folderNoteId) {
        List<CourseChapterDomain> videoChapterDomainList = new ArrayList<>();
        String[] split = folderNoteId.split("_");
        if ("T".equals(split[0])) {
            long templateId = Long.parseLong(split[1]);
            List<TemplateChapter> templateChapterList = templateChapterRepository.findByTemplateIdAndDeleteFlag(templateId, 0);
            if (templateChapterList!=null) {
                for (TemplateChapter templateChapter : templateChapterList) {
                    if (templateChapter.getType()!=null&&templateChapter.getType().equals(10)) {
                        String templateChapterId = templateChapter.getId().toString();
                        CourseChapterDomain videoChapterDomain = new CourseChapterDomain();
                        videoChapterDomain.setChapterNoteId("T_" + templateChapterId);
                        videoChapterDomain.setChapterName(templateChapter.getName());
                        videoChapterDomainList.add(videoChapterDomain);
                    }
                }
            }
        }
        return videoChapterDomainList;
    }

    /**
     * 获取章下的所有节名称(添加文件时用)
     *
     * @param chapterNoteId 章名标识
     * @return
     */
    public List<CourseChapterDomain> getVideoSectionDomain(String chapterNoteId) {
        List<CourseChapterDomain> domainList = new ArrayList<>();
        String[] split = chapterNoteId.split("_");
        if ("T".equals(split[0])) {
                Long templateId = Long.parseLong(split[1]);
                List<TemplateChapter> templateChapterList = templateChapterRepository.findByParentIdAndDeleteFlag(templateId, 0);
                for (TemplateChapter templateChapter : templateChapterList) {
                    if (templateChapter.getType() == 20) {
                        CourseChapterDomain domain = new CourseChapterDomain();
                        String chapterId = String.valueOf(templateChapter.getId());
                        domain.setChapterNoteId("T_" + chapterId);
                        domain.setChapterName(templateChapter.getName());
                        domainList.add(domain);
                    }
                }
            }
        return domainList;
    }

    /**
     * 文件上传
     *
     * @param domain
     */
    public void uploadFile(FileUploadDomain domain) {
        String folderNoteId = domain.getFolderNoteId();
        String[] split = folderNoteId.split("_");
        if ("D".equals(split[0])) {
            Long dirId = Long.parseLong(split[1]);
            DirFile dirFile = new DirFile();
            dirFile.setDirId(dirId);//文件夹id
            dirFile.setFileUrl(domain.getFileUrl());//路径
            dirFile.setName(domain.getFileName());//文件名
            dirFile.setDeleteFlag(0);
            dirFile.setType(domain.getType());
            dirFile.setCreatedDate(new Date());
            dirFileRepository.save(dirFile);
        } else if ("T".equals(split[0])) {
            if (domain.getChapterNoteId() != null && domain.getSectionNoteId() != null) {
                TemplateChapterFile templateChapterFile = new TemplateChapterFile();
                Long sectionId = Long.parseLong(domain.getSectionNoteId().split("_")[1]);
                templateChapterFile.setChapterId(sectionId);
                templateChapterFile.setFileUrl(domain.getFileUrl());
                templateChapterFile.setName(domain.getFileName());
                templateChapterFile.setDeleteFlag(0);
                templateChapterFile.setType(domain.getType());
                templateChapterFile.setCreatedDate(new Date());
                templateChapterFileRepository.save(templateChapterFile);
            } else {
                TemplateChapterFile templateChapterFile = new TemplateChapterFile();
                Long chapterNoteId = Long.parseLong(domain.getChapterNoteId().split("_")[1]);
                templateChapterFile.setChapterId(chapterNoteId);
                templateChapterFile.setFileUrl(domain.getFileUrl());
                templateChapterFile.setName(domain.getFileName());
                templateChapterFile.setDeleteFlag(0);
                templateChapterFile.setType(domain.getType());
                templateChapterFile.setCreatedDate(new Date());
                templateChapterFileRepository.save(templateChapterFile);
            }
        } else {
            if (domain.getChapterNoteId() != null && domain.getSectionNoteId() != null) {
                CourseChapterFile courseChapterFile = new CourseChapterFile();
                Long sectionId = Long.parseLong(domain.getSectionNoteId().split("_")[1]);
                courseChapterFile.setChapterId(sectionId);
                courseChapterFile.setFileUrl(domain.getFileUrl());
                courseChapterFile.setName(domain.getFileName());
                courseChapterFile.setDeleteFlag(0);
                courseChapterFile.setType(domain.getType());
                courseChapterFile.setCreatedDate(new Date());
                courseChapterFile.setIsHide(false);
                courseChapterFileRepository.save(courseChapterFile);
            } else {
                CourseChapterFile courseChapterFile = new CourseChapterFile();
                Long chapterNoteId = Long.parseLong(domain.getChapterNoteId().split("_")[1]);
                courseChapterFile.setChapterId(chapterNoteId);
                courseChapterFile.setFileUrl(domain.getFileUrl());
                courseChapterFile.setName(domain.getFileName());
                courseChapterFile.setDeleteFlag(0);
                courseChapterFile.setType(domain.getType());
                courseChapterFile.setCreatedDate(new Date());
                courseChapterFile.setIsHide(false);
                courseChapterFileRepository.save(courseChapterFile);
            }
        }
    }

    /**
     * 修改课程下的文件
     *
     * @param id
     * @param name
     * @return
     */
    public CourseChapterFile updateCourseChapterFile(Long id, String name) {
        CourseChapterFile courseChapterFile = courseChapterFileRepository.findByIdAndDeleteFlag(id, 0);
        courseChapterFile.setName(name);
        courseChapterFile.setCreatedDate(new Date());
        courseChapterFileRepository.save(courseChapterFile);
        return courseChapterFile;
    }

    /**
     * 修改模板下的文件
     *
     * @param id
     * @param name
     * @return
     */
    public TemplateChapterFile updateTemplateChapterFile(Long id, String name) {
        TemplateChapterFile templateChapterFile = templateChapterFileRepository.findByIdAndDeleteFlag(id, 0);
        templateChapterFile.setName(name);
        templateChapterFile.setCreatedDate(new Date());
        templateChapterFileRepository.save(templateChapterFile);
        return templateChapterFile;
    }

    /**
     * 修改文件夹下的文件
     *
     * @param id
     * @param name
     * @return
     */
    public DirFile updateDirFile(Long id, String name) {
        DirFile dirFile = dirFileRepository.findByIdAndDeleteFlag(id, 0);
        dirFile.setName(name);
        dirFile.setCreatedDate(new Date());
        dirFileRepository.save(dirFile);
        return dirFile;
    }

    /**
     * 对文件进行逻辑删除
     *
     * @param fileNoteId
     */
    public void deleteFile(String fileNoteId) {
        String[] split = fileNoteId.split("_");
        if ("C".equals(split[0])) {
            Long fileId = Long.parseLong(split[1]);
            CourseChapterFile courseChapterFile = courseChapterFileRepository.findByIdAndDeleteFlag(fileId, 0);
            courseChapterFile.setDeleteFlag(1);
            courseChapterFileRepository.save(courseChapterFile);
        } else if ("T".equals(split[0])) {
            Long fileId = Long.parseLong(split[1]);
            TemplateChapterFile templateChapterFile = templateChapterFileRepository.findByIdAndDeleteFlag(fileId, 0);
            templateChapterFile.setDeleteFlag(1);
            templateChapterFileRepository.save(templateChapterFile);
        }else if ("A".equals(split[0])) {
            Long courseId = Long.parseLong(split[1]);
            CourseFile courseFile = courseFileRepository.findById(courseId);
            courseFile.setDeleteFlag(1);
            courseFileRepository.save(courseFile);
        } else if ("B".equals(split[0])) {
            Long templateId = Long.parseLong(split[1]);
            TemplateFile templateFile = templateFileRepository.findById(templateId);
            templateFile.setDeleteFlag(1);
            templateFileRepository.save(templateFile);
        } else if ("E".equals(split[0])){
            videoService.deleteAttFile(Long.parseLong(split[1]));
        } else {
            Long fileId = Long.parseLong(split[1]);
            DirFile dirFile = dirFileRepository.findByIdAndDeleteFlag(fileId, 0);
            dirFile.setDeleteFlag(1);
            dirFileRepository.save(dirFile);
        }


    }

    /**
     * 获取模板文件信息
     * @param id
     * @return
     */
    public TemplateChapterFile getTemplateChapterFile(Long id){
        return templateChapterFileRepository.findByIdAndDeleteFlag(id,0);
    }

    /**
     * 获取文件信息
     * @param id
     * @return
     */
    public DirFile getDirFile(Long id){
        return dirFileRepository.findByIdAndDeleteFlag(id,0);
    }

    /**
     * 获取课程文件信息
     * @param id
     * @return
     */
    public CourseChapterFile getCourseChapterFile(Long id){
        return courseChapterFileRepository.findByIdAndDeleteFlag(id,0);
    }
    public CourseFile updateCourseFile(Long id,String name){
        CourseFile courseFile = courseFileRepository.findById(id);
        courseFile.setName(name);
        return courseFileRepository.save(courseFile);
    }
    public TemplateFile updateTemplateFile(Long id,String name){
        TemplateFile templateFile = templateFileRepository.findById(id);
        templateFile.setName(name);
        return templateFileRepository.save(templateFile);
    }
    public CourseFile findCourseFile(Long id){
        return courseFileRepository.findById(id);
    }
    public TemplateFile findTemplateFile(Long id){
        return templateFileRepository.findById(id);
    }
}
